package com.blb.missiom.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.missiom.entity.Permission;
import com.blb.missiom.service.IPermissionService;
import com.blb.missiom.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lvtian
 * @date 2023/2/2 17:05
 */
@PreAuthorize("hasAuthority('权限管理') and hasRole('ROLE_管理员')")
@RestController
public class UserController {
    @Autowired
    private IUserService userService;

    @Autowired
    private IPermissionService permissionService;
    //查询所有权限
    @GetMapping("/permissions")
    public List<Permission> getAllPermissions(){
        return permissionService.getPermissions();
    }
    //分页查询所有权限

    @GetMapping("/permissions/page")
    public List<Permission> getPermissionsPage(Integer current , Integer pageSize){
        Page<Permission> permissionsPage = permissionService.getPermissionsPage(new Page<Permission>(current,pageSize));
        return permissionsPage.getRecords();
    }
    //按id查权限
    @GetMapping("/permission/{id}")
    public Permission getPermissions(@PathVariable Long id){
        Permission permissionById = permissionService.getPermissionById(id);
        return permissionById;
    }

    //添加权限
    @PostMapping("/permission")
    public String addPermission (@RequestBody Permission permission){
        return permissionService.addPermission(permission);
    }
    //删除权限
    @DeleteMapping("/permission/{id}")
    public String deletePermission(@PathVariable Long id){
        return permissionService.removePermission(id);
    }

    //修改权限
    @PutMapping("/permission")
    public String updatePermission(@RequestBody Permission permission){
        Permission findById = permissionService.getById(permission.getId());
        if(findById == null){
            return "该对象不存在";
        }else {
            return permissionService.updatePermission(permission);
        }
    }
}
